/**
 * Looker  新闻整合系统
 * Author  :solosky <solosky772@qq.com>
 * File    :Data.js
 * Date    :August 5, 2009
 * License : Apache License 2.0 
 */

/**
 *  小工具包
 *  定义了小工具的管理操作，创建，显示，隐藏，销毁等
 */
  
  
  /**
   * 基本小工具
   * 最基本的小工具，包含一个标题框，编辑框和正文框
   * 所有的小工具都继承自这个类
   */
   var BasicWidget = function(settings){
	   
	/////////////////////////////////////////////////  
	//widget默认配置
	//this.settings = {
//		id    : "widget-3",
//		title : "小工具",
//		name  : "NewsSlideWidget",
//		type  : "BasicWidget",
//		content:"content.html",
//		configs : {
//			title:{type:"text",name:"title",defaults:"小工具",display:"小工具标题",value:"小工具"},
//			autoload:{type:"select",name:"autoload",defaults:"1",value:'0',display:"是否自动加载",options:[
//					{value:"0",display:"是"},
//					{value:"1",display:"不是"}
//				]
//			}
//			},
//		styles: ["widget","movable","collapsable","removable","editable","closeconfirm"],
//		callbacks : {
//				onClose  : null,	//用户关闭时触发
//				afterLoad : "afterLoad"	,	//在渲染后时触发
//				afterSave : null
//		},
//		position:{
//			tabId   : "index",
//			placeId : "widget-place-1",
//			order   :""
//		},
//		about :{
//			author : "",
//			site   : "",
//			email  : "",
//			time   : "",
//			intro  : ""
//		}
//	};
	this.settings = settings;
	/////////////////////////////////////////////////
	//基本的模版
	 this.tpl = '<div id="{widgetId}" class="{widgetStyles}">\n'+
					  '<div class="widget-header"> <strong>{widgetTitle}</strong> </div>\n'+
					  '<div class="widget-editbox">\n'+
					  '<form name="widget-editbox-form">'+
					  '		<div class="widget-editbox-fields">{widgetEditFields}<div class="clear"></div></div>\n'+
				  	  '		<div class="widget-editbox-buttons">\n'+
				  	  '			<input type="button"  name="YES" value="确定" class="input-yes" />\n'+
				  	  '			<input type="button"  name="NO" value="取消" class="input-no"/>\n'+
				      '		</div>\n'+
					  '</form>\n'+
					  '</div>\n'+
					  '<div class="widget-content">\n'+
					  '		<div class="widget-content-detail"> </div>\n'+
					  '		<div class="widget-content-loading"><span><img src="./images/loading.gif"></span></div>\n'+
					  '</div>\n'+
				'</div>\n';
	//包装后的jquery对象
	this.$widget = null;
	
	
	
	///////////////////////////////////////////////////
	//销毁
	this.destroy = function(){
		var s = this.settings;
		$("#"+s.id).remove();
		delete this.settings;
	};
	//返回widget编号
	this.getID = function(){
		return this.settings.id;	
	};
	//设置编号
	this.setID = function(widgetId){
		this.settings.id = widgetId;
	};
	//设置标题
	this.setTitle = function(title,notSave){
		this.$widget.find(".widget-header strong").html(title);
		this.title = title;
		if(this.settings.configs.title)
			this.settings.configs.title.value=title;
		if(!notSave)
			TabManager.getActiveTab().saveTabSettings();
	};
	this.getTitle = function(){
		return this.title;
	};
	this.getPosition = function(){
		return this.settings.position;
	};
	//得到小工具配置
	this.getConfig = function(name){
		var c = this.settings.configs;
		return c[name]? c[name].value : null;
	};
	//设置小工具配置
	this.setConfig = function(name, value){
		var c = this.settings.configs;
		if(name=="title" && this.settings.configs.title)
			this.setTitle(value);
		else
			if(c[name]) c[name].value = value;
	};
	//渲染小工具基本外形
	this.render = function(){
		var t = this.tpl;
		var s = this.settings;
		t = t.replace("{widgetStyles}",s.style);
		t = t.replace("{widgetId}", s.id);
		t = t.replace("{widgetTitle}", s.title);
		t = t.replace("{widgetEditFields}", ConfigsHelper.parse(s.configs));
		
		//用jquery包装html
		this.$widget = $(t);
		var self = this;
		//绑定编辑回调事件
		this.$widget.find(".widget-editbox-buttons input[name='YES']").click(
			function(){
				var values = Util.Form.values(self.$widget.find("form").get(0));
				var configs = self.settings.configs;
				for(var v in values){
						if(configs[v])
							configs[v].value = values[v];
				}
				if(configs.title)
					self.setTitle(configs.title.value, true);
				//保存配置至cookie
				TabManager.getActiveTab().saveTabSettings();
				self.$widget.find(".widget-editbox").slideUp(function(){
					//回调用户自定义的在编辑后触发的函数
					if(self.settings.callbacks.afterSave)
						self.settings.callbacks.afterSave(this);
					//重新加载正文
					self.load();
				});
			}
		);
		this.$widget.find(".widget-editbox-buttons input[name='NO']").click(
			function(){
				self.$widget.find(".widget-editbox").slideUp();				
			}
		);
		//显示widget
		 $.fn.AddEasyWidget(this.$widget,this.settings.position.placeId, WidgetHelper.getDefaultWidgetOption() );
	};
	//返回小工具的路径
	this.baseURL = function(){
		return Config.Path.Index+Config.Path.Widget+this.settings.name+"/";
	};
	//设置正文内容
	this.setContent = function(html){
		this.$widget.find(".widget-content-detail").html(html);
	};
	//显示/或者隐藏一个loading层，
	this.overlay = function(isShow){
//		var loading = this.$widget.find(".widget-content-loading");
//		var content = this.$widget.find(".widget-content");
//		if(isShow){
//			//显示
//			if(content.height()<45) content.css("height","50px");
//			var co = content.offset();l(co);
//			//loading.css("position","absolute");
////			loading.css("top", co.top+"px");
////			loading.css("left", co.left+"px");
////			loading.css("height",content.height()+10+"px");
////			loading.css("width",content.width()+10+"px");
//			loading.css({
//				position:"absolute",
//				top		: co.top+"px",
//				left	: co.left+"px",
//				height  : content.height()+"px",
//				width   : content.width()+"px"
//				});
//			var span = loading.children("span");
//			var spanTop = (content.height()-40)/2;
//			var spanLeft = (content.width()-40)/2;
//			span.css("margin-top",spanTop+"px");
//			span.css("margin-left",spanLeft+"px");
//			loading.show();	
//		}else{
//		//隐藏	
//			loading.hide();
//			content.css("height","auto");
//			content.css("position","relative");
//		}
	},
	//载入小工具正文
	this.load = function(){
		if(this.settings.content){
			var self = this;
			var suc = function(data){
				self.overlay(false);
				self.setContent(data);
					try{
						if(self.settings.callbacks.afterLoad){
							var code = self.settings.callbacks.afterLoad+"(self);";
							eval(code);
						}
					}catch(e){
						Logger.error("Widget.afterLoad: "+e);
					}
				};
			var err = function(){
				self.overlay(false);
				var html = $("<div>载入失败，请<a href=#>重试</a></div>");
				html.find("a").click(function(){
					Logger.info(self.getID()+" reload....");
					self.load();
					return false;
											  
				});	
				self.setContent(html);
			};
			this.overlay(true);
			RequestManager.addRequest(new ServerRequest({url:this.baseURL()+this.settings.content,dataType:"html",success:suc, error:err}));
			
		}
		
	},
	//输出widget
	this.display = function(){
		this.render();
		this.load();
		Logger.info("Basic.display: runrun");
	};
	
};
   
   
   
   /**
   * 标签类
   * 标签类是所有小工具的容器，管理着一组小工具s
   * 之所以叫标签类，主要是和页面导航条的标签对应
   */
   var Tab = function(tabID) {
	   
	   //////////////////////////////////////////////////////////////////
	   //Tab下面所有小工具
	   this.widgetList = new Array();
	   //tabID
	   this.tabID = tabID;
	   //tab配置，包含了下面所有的widget配置
	   this.tabSettings = {
	   };
	   //保存的tab配置
	   this.savedSettings = {};
	   
	   ////////////////////////////////////////////////////////////////////////
	   //id
	   this.getTabID = function(){
		 return this.tabID;  
	   };
	   //初始化tab
	   this.init = function(){
		   Logger.debug("Tab.init ...");
		   TabManager.loadingTip(true);
		   if(!this.readTabSettings())
		   	  	this.loadDefaultTabSettings();
			else
			   	this.loadWidgetsSettings();
	   };
	   //恢复默认的widget
	   this.restore = function(){
		   var wl = this.widgetList;
		 	for(var i=0;i<wl.length; i++){
				wl[i].destroy();
				delete wl[i];
			}
			wl.length=0;
		   this.deleteTabSettings();
		   TabManager.loadingTip(true);
		   this.loadDefaultTabSettings();
		   
	   };
	   //销毁这个tab,删除widget
	   this.destroy = function(){
		   var wl = this.widgetList;
		 	for(var i=0;i<wl.length; i++){
				wl[i].destroy();
				delete wl[i];
			}
			delete this.widgetList;
			delete this.tabSettings;
			delete this.savedSettings;
	   };
	   this.deleteWidget = function(widgetId){
		   var w = this.findWidget(widgetId);
		   if(w){
		   		w.destroy();
				Util.Arr.del(this.widgetList,w,function(a,b){return a.getID()==b.getID();});
				this.saveTabSettings();
		   }
			
	   };
	   //查找指定widgetID的widget 对象
	   this.findWidget = function(widgetId){
			var i = 0;
			for(i=0; i<this.widgetList.length; i++){
				if(this.widgetList[i].settings.id==widgetId)
					return this.widgetList[i];
			}
			Logger.warn("Tab.findWidget 编号为 "+widgetId+" 的widget不存在");
			return null;
		   
	   },
	   //返回代表当前位置信息的字符串
	   this.generateWidgetsPositions = function(){
			var positions = {};
			var p = new Util.Str.Builder();
			//首先要根据不同的位置分组
			for(var i=0; i<this.widgetList.length; i++){
				var cp = this.widgetList[i].settings.position;
				if(!positions[cp.placeId])
					positions[cp.placeId] = new Array();
				positions[cp.placeId].push({id:this.widgetList[i].settings.id,order:parseInt(cp.order)});
			}
			//然后每个组排序,并输出位置字符串
			for(var v in positions){
				var tp = positions[v].sort(function(a,b){return a.order > b.order;});
				var b = new Util.Str.Builder();
				for(var m in tp)
					b.append(tp[m].id);
				p.append(v+"="+b.join(","));
			};
			return p.join("|");
			
	   };
	   //更新当前所有widget的位置信息
	   this.updateWidgetsPositions = function(positions){
		   var places = positions.split("|");
		   for(var i=0; i<places.length;i++){
			   
			    var pp = places[i].substring(0,places[i].indexOf("="));
				var wp = places[i].substring( places[i].indexOf("=")+1);
				if(wp.length<1)
					continue;
				wp = wp.split(",");
				var cw = null;
				for(var j=0;j<wp.length;j++){
					if(cw = this.findWidget(wp[j])){
						cw.settings.position.placeId = pp;
						cw.settings.position.order = j;
					}
				}
		   }
		   
	   };
	   //合并当前tab下面widget的默认配置和用户定义的配置
	   this.mergeTabSettings = function(widgetsSettings){
		   var ss = this.savedSettings;
		   var ws = widgetsSettings;
		   var ts = {};
		   
		   
		   //首先要复制这个tab的基本信息
		   ts.id = ss.id;
		   ts.title = ss.title;
		   ts.widgets = new Array();
		   
		   //合并用户保存的这个tab下所有widget的配置信息和widget完整的配置信息
		   for(var i=0;i<ss.widgets.length; i++){
			   var cw = ss.widgets[i];
			   var nw = {};	//新的widget配置信息
			   //从用户保存的配置信息里获取id,title, name，position
			   nw.id = cw.id;
			   nw.title = cw.title;
			   nw.name = cw.name;
			   nw.position = cw.position;
			   //$.extend(nw.position, cw.position);
			   //从服务器获取的配置信息里获取type,style,content,configs,callbacks,about
			   nw.type = ws[ nw.name ].type;
			   nw.style = ws[ nw.name ].style;
			   nw.content = ws[ nw.name ].content;
				//这里复制的东西是数组或者对象，这里很奇怪，如果直接用$.extend(true,nw.configs, ws[ nw.name ].configs);就会失败，
				//必须要借助一个中间变量才成。。
				var tmp  = {};
				$.extend(true,tmp, ws[ nw.name ].configs);
				nw.configs = tmp;
				tmp = {};
				$.extend(true,tmp, ws[ nw.name ].about);
				nw.about = tmp;
				tmp = {};
				$.extend(true,tmp, ws[ nw.name ].callbacks);
				nw.callbacks = tmp;
			   //更新用户的配置信息
			  var cc = ss.widgets[i].configs;
			   for(var c in cc){
					nw.configs[c].value = cc[c];   
			   }
			   if(cc.title)
			   		nw.title = cc.title;
			   //把一个新的widget配置添加到当前tab的widget配置列表中
			   ts.widgets.push(nw);
		   }
		   //设置新的tab配置
		   this.tabSettings = ts;
		   //Logger.debug("tab.mergeTabSettings : "+Util.Json.encode(ts));
	   };
	   //保存当前所有widget的配置
	   this.saveTabSettings = function(){
		   var s = this.tabSettings;
		   var ws = this.widgetList;
		   var o = {
			   id:s.id,
			   title:s.title,
			   widgets:new Array()};
			   for(var i=0; i<ws.length;i++){
			   		var t ={
						   id:ws[i].settings.id,
						   title:ws[i].settings.title,
						   name : ws[i].settings.name,
						   configs:{},
						   position:ws[i].settings.position
						   };
					for(var c in ws[i].settings.configs){
						t.configs[c] = ws[i].settings.configs[c].value;
					}
					o.widgets.push(t);
			   }
		   Logger.debug("Tab.saveTabSettings: "+Util.Json.encode(o));
		   Util.Cookie.set("LOOKER_TAB_"+this.tabID, Util.Json.encode(o));
	   };
	   //清除用户保存的配置
	   this.deleteTabSettings = function(){
		 	Util.Cookie.set("LOOKER_TAB_"+this.tabID,null);  
	   };
	   //从cookie中获取当前tab的widget配置
	   this.readTabSettings = function(){
		   var t = Util.Cookie.get("LOOKER_TAB_"+this.tabID);
		   if(t) this.savedSettings = Util.Json.decode(t);
		   Logger.debug("Tab ReadTabSettingsFromCookie:"+Util.Json.encode(this.tabSettings));
		   return t ? true : false;
	   };
	   //从服务器下载完整的widget配置
	   this.loadWidgetsSettings = function(){
		   //统计当前tab所有的widget类型
		   var hash  = {};
		   var names = new Array();
		   var widgets = this.savedSettings.widgets;
		   for(var i=0; i<widgets.length; i++){
			   var w = widgets[i];
				if(!hash[w.name]){
					hash[w.name]=true;
					names.push(w.name);
				}
		   }
		   
		   var self = this;
		   var suc  = function(data){
			   self.mergeTabSettings(data);
			   self.prepareWidgets();
			   TabManager.loadingTip(false);
			   self.displayWidgets();
		   };
		   var request = new ServerRequest({
							   url:Config.URL.WidgetInfo.replace("{names}",names.join("|")),
							   success:suc,
							   dataType : "json"
								});
		   RequestManager.addRequest(request);
		   
	   };
	   //载入当前tab的默认widget配置
	   this.loadDefaultTabSettings = function(){
		   var self = this;
		   var suc  = function(data){
			   self.savedSettings = data;
			   Logger.debug("Tab.loadDefaultTabSettings :"+Util.Json.encode(data));
			   self.loadWidgetsSettings();	  
		   };
		   var request = new ServerRequest({
							   url:Config.URL.DefaultTabSettings.replace("{tabID}",this.tabID),
							   success:suc,
							   dataType : "json"
								});
		   RequestManager.addRequest(request);
	   },
	   //显示所有的widget
	   this.displayWidgets = function(){
		    for(var i=0; i<this.widgetList.length; i++){
				this.widgetList[i].display();
		   }
	   };
	   //准备所有的widget
	   this.prepareWidgets = function(){
		   var widget = null;
		   var setting= null;
		   var code   = null;
		   //$.fn.EasyWidgets(WidgetHelper.getDefaultWidgetOption());
		   for(var i=0; i<this.tabSettings.widgets.length; i++){
				this.prepareWidget(this.tabSettings.widgets[i]);
		   }
	   };
	   //准备widget
	   this.prepareWidget = function(settings){
		   var widget = null;
		   var res = null;
		   var code = "widget = new "+settings.type+"(settings);";
			try{
				eval(code);	
			}catch(e){
				Logger.error("Tab.DisplayWidgets:"+settings.type+" "+e);	
			}
			//widget.display();
			this.widgetList.push(widget);
			return widget;
	   };
	   //添加一个widget
	   this.addWidget = function(settings){
		   this.tabSettings.widgets.push(settings);
		   this.prepareWidget(settings).display();
		   this.saveTabSettings();
		   Logger.info("Tab.addWidget 成功添加widget");
	   };
   };
   
   
  /**
   * 标签管理类
   * 管理标签的创建和销毁等
   */
   var TabManager = {
	   
	   //当前tab
	   activeTab : null,
	   //所有可用的tab
	   tabGroupSettings  : null,
	   //widgetholder选择器
	   holderSelector: ".widget-holder",
	   ///tab选择器
	   tabSelector : "div.tab",
	   //tabHTML标记
	   $tab : null,
	   
	   
	   //初始化,最先调用的函数
	   init : function(){
		 this.readTabGroupSettings();
		 this.render();
		 this.loadTab(this.tabGroupSettings.IndexTab);
	   },
	   //返回当前tab
	   getActiveTab : function(){
		   return this.activeTab;
	   },
	   //读取保存在cookie的所有tab列表
	   readTabGroupSettings : function(){
			var t = Util.Cookie.get(Config.Cookie.prex+"TabList");
			this.tabGroupSettings =  t ? Util.Json.decode(t): Config.Tab;
			//Logger.debug("TabManager TabGroupSetting:"+Util.Json.encode(this.tabGroupSettings));
			if(!t) this.saveTabGroupSettings();
	   },
	   //保存tab列表
	   saveTabGroupSettings : function(){
		 	Util.Cookie.set(Config.Cookie.prex+"TabList",Util.Json.encode(this.tabGroupSettings));  
	   },
	   //渲染输出顶部导航标签
	   render : function(){
		   		var tabs = this.tabGroupSettings.Tabs;
				var t = this.$tab = $(this.tabSelector);
			   for(var i=0; i<tabs.length;i++)
				    t.find(".tab-header").append("<a href='#' name='"+tabs[i].id+"' class='tab-link-off' onclick='TabManager.loadTab(\""+tabs[i].id+"\");return false;'>"+tabs[i].title+"</a>");
				t.find(".tab-footer a[name='add']").click(function(){WidgetHelper.addWidgetDialog();return false;});
				t.find(".tab-footer a[name='restore']").click(function(){
														Message.confirm("你确认恢复当前标签的默认配置吗？","恢复确认?","confirm",
																		function(){TabManager.getActiveTab().restore();});
														return false;
												});
				
	   },
	 	//高亮显示当前标签
		highlight : function(tabID){
			this.$tab.find(".tab-header a").each(function(i){							  
				if(this.name==tabID){
					this.className = "tab-link-on";
				}else{
					this.className = "tab-link-off";	
				}
			});
			
		},
	   //加载Tab
	   loadTab : function(tabID){
		   if(this.activeTab)
		   		this.activeTab.destroy();
		   for(var i=0;i<this.tabGroupSettings.Tabs.length; i++){
				if(tabID==this.tabGroupSettings.Tabs[i].id){
				     this.highlight(tabID);
					 this.activeTab = new Tab(tabID);
					 this.activeTab.init();
					 break;
				}
		   }
		   if(i==this.tabGroupSettings.Tabs.length)
		  	 Logger.warn("TabManager.loadTab "+ tabID + "不存在");
		  
	   },
	   //显示正在加载提示
	   loadingTip : function (isShow){
			   if(isShow){
			   	  $(".widget-holder").hide();
				  $(".widget-loading").fadeIn(1500);
				
			   }else{
				  // $(".widget-loading").fadeOut(2000,function(){ $(".widget-holder").show();});
				  $(".widget-loading").hide();
				  $(".widget-holder").show();
				 
			   }
			   
	   }
	   
   };
   
  /**
   * 小工具帮助工具
   * 拦截对工具回调函数的调用并转发到真正的函数上去``
   */
   var WidgetHelper = {
	   //////////////////////////////////////////////////////////////////////////////////////////////////
	   ProxyOptions : {
		   
		   		//因为easywidget里的函数是全局函数，为了可以调用用户自定义的回调函数，这里需要使用一个代理来转发
				callbacks : {
					//添加时触发的函数
					onAdd : function($widget, placeId){
						//var widgetId = WidgetHelper.getWidgetID($widget);
//						var widget = TabManager.getActiveTab().findWidget(widgetId);
//						if(widget && widget.callbacks.onLoad)
//							widget.callbacks.onLoad(widget);	//触发定义的onLoad事件
					//alert("OnAdd");
					},
					
					//显示时触发的函数
					onShow : function(){
						
					},
					onClose : function(link,$widget){
						var widgetId = WidgetHelper.getWidgetID($widget);
						var widget = TabManager.getActiveTab().deleteWidget(widgetId);
					},
					//当用户拖动小工具后触发，需保存当前位置信息至cookies中
					onChangePositions : function(str){
						Logger.debug("WidgetHelper onChangePositions "+str);
						var t = TabManager.getActiveTab();
						if(t){
							t.updateWidgetsPositions(str);
							t.saveTabSettings();
						}
					},
					//初始化时添加小工具时触发，需返一个表示当前widget位置信息的字符串
					onRefreshPositions : function(){
						var t = TabManager.getActiveTab().generateWidgetsPositions() ;
						Logger.debug("WidgetHelper onRefreshPositions "+t);
						return t;
					}
				}
	   },
	   
	   //////////////////////////////////////////////////////////////
	   //查询给定widget对象的ＩＤ
	   getWidgetID : function($widget){
			return $widget ? $widget.attr("id") : "";		
	   },
	   //生成widgetID
	   generateWidgetID : function(){
		   var d = new Date();
		   return "widget-"+d.getTime();
	   },
		   
	   //返回定义的默认配置
	   getDefaultWidgetOption : function(){
		   var p = WidgetHelper.ProxyOptions;//alert(Util.Json.encode(p));
		  	$.extend(true,p,Config.Widget.defaults);
			return p;
	   },
	   //添加小工具
	   addWidgetAction : function(title,name){
			   Message.confirm("你确定添加[<b> "+title+"</b> ]在当前的标签上吗？",
								"添加小工具确认","confirm",function(){
										 Message.info("正在添加[<b> "+title+"</b> ]在当前的标签上,请稍候..",
															"添加小工具","load",null,0,false);
										 var suc = function(data){
											 //添加一些信息
											 var s = data[name];
											 s.id = WidgetHelper.generateWidgetID();
											 s.position = {
												tabId : TabManager.getActiveTab().getTabID(),
												placeId : 'widget-place-left',
												order : 99
											 };
											 TabManager.getActiveTab().addWidget(s);
											  Message.activeMessage.setMessage("添加[<b> "+title+"</b> ] 在当前标签上加成功","ok");
											 
										 };
										 var err = function(data){
											 Message.activeMessage.setMessage("添加[<b> "+title+"</b> ] 在当前标签上失败，请重试","ok"); 
										 };
										 var request = new ServerRequest({
											url : Config.Path.Index+Config.URL.WidgetInfo.replace("{names}",name),
											dataType : "json",
											success  : suc,
											error    : err 
										});
										 RequestManager.addRequest(request);
									},null,false);
	   },
	    //添加小工具对话框
	   addWidgetDialog : function(){
		   var options = {
			 		title:"添加小工具",
					width:600,
					height:400,
					url:Config.Path.Index+"server/addWidget.php",
					buttons:[
							 {type:"ok",	title:"确定", callback:null},
							 {type:"cancel",title:"取消", callback:null}
						 ]
		   };
		   var diag = new Dialog(options);
		   diag.show();
		   
	   }
   };
   
   /**
   * 小工具配置帮助工具
   * 解析配置，并渲染配置条目
   */
   var ConfigsHelper = {
	 	
		//解析器
		parser :{
				//文本输入框解析器
				text : function(s){
					var b = new Util.Str.Builder();
					b.append("<label>");
					b.append(s.display);
					b.append('</label><span><input type="text" name="');
					b.append(s.name);
					b.append('" value="');
					b.append(s.value?s.value:s.defaults);
					b.append('" /></span>');
					return b.toString();
				},
				//下拉选项解析器
				select : function(s){
					var b = new Util.Str.Builder();
					b.append('<label>');
					b.append(s.display);
					b.append('</label><span><select name="');
					b.append(s.name);
					b.append('">');
					var v = s.value?s.value:s.defaults;
					for(var o in s.options){
						b.append('<option value="');
						b.append(o);
						b.append(v==o ? '" selected="selected" >' : '" >');
						b.append(s.options[o]);
						b.append('</option>');
					}
					b.append('</select>');
					b.append('</span>');
					return b.toString();
				},
				bool:function(s){
					var b = new Util.Str.Builder();
					b.append('<label>');
					b.append(s.display);
					b.append('</label><span><select name="');
					b.append(s.name);
					b.append('">');
					if(s.value=="true"){
						b.append('<option value="true" selected="selected">是</option>');
						b.append('<option value="false">否</option>');
					}else{
						b.append('<option value="true">是</option>');
						b.append('<option value="false" selected="selected">否</option>');
					}
					b.append("</select>");
					b.append("</span>");
					return b.toString();
				},
				title : function(s){
					return this.text(s);
				},
				category : function(s){
					var b = new Util.Str.Builder();
					b.append("<label>");
					b.append(s.display);
					b.append('</label><span>');
					b.append('<select name="'+s.name+'">');
					var c = Config.News.Categorys;
					var tv = s.value.split("-");
					var v = {};
					for(var i=0; i<tv.length; i++){
						v[tv[i]] = true;	
					}
					for(var site in c){
						b.append('<option value="');
						b.append('s-'+c[site].sitehash);
						b.append(('s-'+c[site].sitehash)==s.value ? '" selected="selected" >' : '" >');
						b.append(c[site].title+" - 所有新闻");
						b.append('</option>');
						var list = c[site].categorys;
						for(var i=0; i<list.length; i++){
								b.append('<option value="');
								b.append('c-'+list[i].categoryhash);
								b.append(('c-'+list[i].categoryhash)==s.value ? '" selected="selected" >' : '" >');
								b.append(c[site].title+'-'+list[i].title);
								b.append("</option>");
						}
					}
					b.append('</select>');
					b.append("</span>");
					return b.toString();
				}
		},
		parse : function(settings){
			var b = new Util.Str.Builder();
			for (var v in settings){
				b.append("<div>");;
				b.append(ConfigsHelper.parser[settings[v].type](settings[v]));
				b.append("</div>\n");
			}
			return b.toString();
		}
   };
   
   
   //注册命名空间
   //LOOKER.registerNamespace("Widget");